package android.slc.extras.component.tree.ui

import android.os.Bundle
import android.slc.appbase.ui.vd.BaseListCommonVmBox
import android.slc.core.domain.BundleBuilder
import android.slc.core.vm.SingleLiveEvent
import android.slc.extras.component.tree.config.ConstantsTree
import android.slc.extras.component.tree.entity.SlcTree
import com.blankj.utilcode.util.CollectionUtils
import java.io.Serializable

/**
 * 树列表
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 2:42
 *
 */
abstract class TreeListVmBox<T : SlcTree> : BaseListCommonVmBox<T>() {
    //父节点传递的bundle用于将部分配置属性传递到下个界面
    lateinit var bundle: Bundle
        private set

    //树名称
    val treeName: String?
        get() = bundle.getString(ConstantsTree.KEY_TREE_NAME)

    //有treeId根据treeId来查询子集
    protected var mTreeId: String = ConstantsTree.VALUE_DEF_TREE_ID
        private set

    //没有treeId就根据leafIdList来查询子集
    protected var mLeafIdList = mutableListOf<Serializable>()
        private set

    //显示tree点击后的UI
    val showTreeClickUiOf = SingleLiveEvent<Bundle>()

    open fun initVmBox(bundle: Bundle) {
        this.bundle = bundle
        //初始化treeId
        this.mTreeId =
            bundle.getString(ConstantsTree.KEY_TREE_ID) ?: ConstantsTree.VALUE_DEF_TREE_ID
        //初始化treeIdList
        bundle.getSerializable(ConstantsTree.KEY_LEAF_ID_LIST)?.let {
            if (it is MutableList<*>) {
                CollectionUtils.forAllDo(it) { _, item ->
                    if (item is Serializable) {
                        mLeafIdList.add(item)
                    }
                }
            }
        }
    }

    protected open fun loadMoreTree() {
        //此处根据treeId和treeIdList来进行选择加载数据，以下是示例
        /*DeptServiceRepository.list(packId).flatMap(object :
            Function<SlcEntity<List<Dept>>, ObservableSource<SlcEntity<DeptAndUser>>> {
            override fun apply(t: SlcEntity<List<Dept>>): ObservableSource<SlcEntity<DeptAndUser>> {
                return UserServiceRepository.list(packId).map { pageModelSlcEntity ->
                    val slcEntity: SlcEntity<DeptAndUser> =
                        DataTransformUtils.slcEntity2TargetRSlcEntity(pageModelSlcEntity)
                    slcEntity.data = DeptAndUser()
                    slcEntity.data.deptId = packId
                    slcEntity.data.userList = pageModelSlcEntity.data
                    slcEntity.data.nodeDeptList = t.data
                    return@map slcEntity
                }.to(bindToLifecycle())
            }
        }).map(DeptServiceRepository.data2ListGroup())
        .map(PageTransformUtils.list2PageModelMap())
            .to(bindToLifecycle())
            .subscribe(listPageWrapper.dataListObserver)*/
    }

    final override fun itemClick(index: Int, data: T) {
        //根据类型来确定相关事件
        when (data.tType) {
            SlcTree.VALUE_TREE -> {
                treeClick(index, data)
            }
            SlcTree.VALUE_LEAF -> {
                leafClick(index, data)
            }
        }
    }

    /**
     * 树点击
     */
    protected open fun treeClick(index: Int, data: T) {
        showTreeClickUiOf.value = BundleBuilder.create().build().apply {
            putAll(this@TreeListVmBox.bundle)
            putSerializable(ConstantsTree.KEY_TREE_ID, data.tId)
            putString(ConstantsTree.KEY_TREE_NAME, data.tName)
        }
    }

    /**
     * 树叶点击
     */
    protected open fun leafClick(index: Int, data: T) {

    }

}